package com.lccviolet;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @author LiChaochao
 * @Date 2021/9/24 19:17
 * @Description 虚引用
 */
public class PhantomReferenceDemo {
    public static void main(String[] args) {
        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        PhantomReference<Object> phantomReference = new PhantomReference<>(o1, referenceQueue);

        System.out.println("o1 = " + o1);
        System.out.println("phantomReference = " + phantomReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());

        System.out.println("================================");
        o1 = null;
        System.gc();
        System.out.println("o1 = " + o1);
        System.out.println("phantomReference = " + phantomReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());

    }
}
